const path = require('path') //引入path模块
function resolve(dir) {
  return path.join(__dirname, dir) //path.join(__dirname)设置绝对路径
}
const { devServer } = require('./dev.config')
const VUE_APP_MODE = process.env.VUE_APP_MODE

module.exports = {
  productionSourceMap: false,
  chainWebpack: config => {
    //cdn
    config.externals({
      axios: 'axios',
      vue: 'Vue',
      'vue-router': 'VueRouter',
      vuex: 'Vuex',
      'element-ui': 'ELEMENT'
    })
    config.resolve.alias
      .set('@api', resolve('./src/http/api/v1'))
      .set('@http', resolve('./src/http/axios'))
      .set('@vue', resolve('./src/vue'))
    config.plugin('html').tap(args => {
      args[0].title = '标题'
      return args
    })

    if (VUE_APP_MODE !== 'dev') {
      config.output
        .filename('js/[chunkhash:8].js')
        .chunkFilename('js/[chunkhash:8].js')
      config.plugin('extract-css').tap(args => {
        args[0] = {
          filename: 'css/[contenthash:8].css',
          chunkFilename: 'css/[contenthash:8].css'
        }
        return args
      })
    }
  },
  css: {
    loaderOptions: {
      scss: {
        prependData: `@import "~@/static/css/mixin.scss";`
      }
    }
  },
  devServer
}
